﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Fa;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Fa
{
    public partial class frmManageAssetUseApplication : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FaUsedApplicationEntity mSelectedUsedApplication;
        #endregion

        #region Constructor
        public frmManageAssetUseApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FUA", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "FUA", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtApplicationCode.Text) == false)
            {
                condition += " AND UA.APPLICATION_CODE LIKE '%" + txtApplicationCode.Text + "%'";
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                condition += " AND UA.APPLICATION_DATE >= CONVERT(DATETIME,'" + dtpApplicationDate.EditValue+ "')";
            }
            if (dtpApplicationDateTo.EditValue!=null)
            {
                condition += " AND UA.APPLICATION_DATE <= CONVERT(DATETIME,'" + dtpApplicationDateTo.EditValue + "')";
            }
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity entity = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND D.DEPARTMENT_ID = '" + entity.DepartmentId + "' ";
            }
            if (mCanQueryOtherPerson)
            {
                if (txtApprovedPerson.Tag != null && txtApprovedPerson.Tag.GetType() == typeof(MstPersonEntity))
                {
                    MstPersonEntity entity = (MstPersonEntity)txtApprovedPerson.Tag;
                    condition += " AND UA.APPLICATION_PERSON = '" + entity.PersonId + "' ";
                }
            }
            else
            {
                condition += " AND UA.APPLICATION_PERSON = '" + CommonParameter.CurrentUser.PersonId + "' ";
            }
            if (txtApprovedPerson.Tag != null && txtApprovedPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtApprovedPerson.Tag;
                condition += " AND UA.APPROVED_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND UA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "UA.APPLICATION_CODE DESC";
            DataTable dt = FaUsedApplicationBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnSend.Enabled = false;
            btnConfirm.Enabled = false;
            if (mSelectedUsedApplication == null)
            {
                return;
            }
            if (mSelectedUsedApplication.Status == "FUA01" || mSelectedUsedApplication.Status == "FUA03")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedUsedApplication.Status == "FUA02")
            {
                btnSend.Enabled = true;
            }
            else if (mSelectedUsedApplication.Status == "FUA04")
            {
                btnConfirm.Enabled = true;
            }
        }
        #endregion

        protected override void OnShown(EventArgs e)
        {
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight("ESOL.WinUI.FA.frmAssetUseApplicationManage", "btnApprove");
            if (UICommon.UICommonFunctions.CheckPersonRight("ESOL.WinUI.FA.frmAssetUseApplicationManage", "btnSend"))
            {
                mCanQueryOtherPerson = true;
            }
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedUsedApplication = new FaUsedApplicationEntity(dr);
            ResetButtonsEnable();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtApprovedPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApprovedPerson.Text = frm.SelectedPerson.NameCn;
                txtApprovedPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApprovedPerson.Text = "";
                txtApprovedPerson.Tag = null;
            }
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtDepartment.Text = "";
                txtDepartment.Tag = null;
            }
        }

        protected override void Click_View()
        {
            if (mSelectedUsedApplication == null)
            {
                MessageBox.Show("请选择一个固定资产使用申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetUseApplication frm = new frmMaintainAssetUseApplication();
            frm.ThisActionType = ActionType.View;
            frm.SelectedUsedApplication = mSelectedUsedApplication;
            frm.ShowDialog();
        }
        protected override void Click_New()
        {
            frmMaintainAssetUseApplication frm = new frmMaintainAssetUseApplication();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectedUsedApplication == null)
            {
                MessageBox.Show("请选择一个固定资产使用申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetUseApplication frm = new frmMaintainAssetUseApplication();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedUsedApplication = mSelectedUsedApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Delete()
        {
            if (mSelectedUsedApplication == null)
            {
                MessageBox.Show("请选择一个固定资产使用申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中固定资产使用申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedUsedApplication.Status = "FUA99";
                bool successful = FaUsedApplicationBs.UpdateFaUsedApplication(mSelectedUsedApplication);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        protected override void Click_Approve()
        {
            if (mSelectedUsedApplication == null)
            {
                MessageBox.Show("请选择一个固定资产使用申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetUseApplication frm = new frmMaintainAssetUseApplication();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedUsedApplication = mSelectedUsedApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (mSelectedUsedApplication == null)
            {
                MessageBox.Show("请选择一个固定资产使用申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否要确认选中固定资产使用申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedUsedApplication.Status = "FUA05";
                mSelectedUsedApplication.ConfirmedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedUsedApplication.ConfirmedDate = DateTime.Now;
                bool successful = FaUsedApplicationBs.ApproveUseApplication(mSelectedUsedApplication);
                if (successful)
                {
                    MessageBox.Show("确认成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (mSelectedUsedApplication == null)
            {
                MessageBox.Show("请选择一个固定资产使用申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetUseApplication frm = new frmMaintainAssetUseApplication();
            frm.ThisActionType = ActionType.CEOApprove;
            frm.SelectedUsedApplication = mSelectedUsedApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

    }
}